//
//  RegisterTwoViewController.m
//  HTP
//
//  Created by 陈钱钿 on 15/11/1.
//  Copyright © 2015年 nit. All rights reserved.
//

#import "RegisterTwoViewController.h"
#import "RegisterThreeViewController.h"
#import "UserVM.h"
@interface RegisterTwoViewController ()
@property (nonatomic, retain) UITextField *Field;

@property (nonatomic,retain) UIButton *timeBtn;
//定时器时间记录
@property (nonatomic,assign) NSInteger seconds;

@property (nonatomic,retain) NSTimer *timer;

@property (nonatomic,retain) NSDate  *sendTime;
@end

@implementation RegisterTwoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _sendTime = [NSDate date];
    _seconds = 60;
    self.view.backgroundColor = [UIColor whiteColor];
    [self initUI];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
     _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(getCardNum:) userInfo:nil repeats:YES];
    [[NSRunLoop  currentRunLoop] addTimer:_timer forMode:NSDefaultRunLoopMode];
    [self addCustomTitleWithTitle:@"验证手机号码"];
    [self addCustomBackBase];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
     [_timer invalidate];
}
//TODO:绘制UI
- (void)initUI{
    self.Field = [[UITextField alloc] initWithFrame:CGRectMake((ScreenWidth-180)/2, ScreenHeihgt/4, 120, 30)];
    self.Field.placeholder = @"请输入验证码";
    self.Field.keyboardType = UIKeyboardTypePhonePad;
    self.Field.font = [UIFont systemFontOfSize:18];
    [self.view addSubview:self.Field];
    
    UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake((ScreenWidth-240)/2, CGRectGetMaxY(self.Field.frame), 150, 2)];
    line.image = [UIImage imageNamed:@"black_line"];
    line.alpha = 0.5;
    [self.view addSubview:line];
    
    _timeBtn = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(line.frame), ScreenHeihgt/4, 90, 32)];
    [_timeBtn setBackgroundImage:[UIImage imageNamed:@"get_again"] forState:UIControlStateNormal];
    [_timeBtn setTitle:@"60秒后重新获取" forState:UIControlStateNormal];
    _timeBtn.enabled = NO;
    _timeBtn.titleLabel.font = [UIFont systemFontOfSize:11.0];
    [_timeBtn addTarget:self action:@selector(getCardNum:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_timeBtn];
    
    UIButton *confirmBtn = [[UIButton alloc] initWithFrame:CGRectMake((ScreenWidth-240)/2, CGRectGetMaxY(_timeBtn.frame)+40, 240, 40)];
    [confirmBtn setBackgroundImage:[UIImage imageNamed:@"confirm"] forState:UIControlStateNormal];
    [confirmBtn addTarget:self action:@selector(confirmClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:confirmBtn];
}
//TODO:Action
/**
 *  验证码函数
 *
 *  @param sender 
 */
-(void)getCardNum:(id)sender{
    if ([sender isKindOfClass:[UIButton class]]) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(getCardNum:) userInfo:nil repeats:YES];
        [[NSRunLoop  currentRunLoop] addTimer:_timer forMode:NSDefaultRunLoopMode];
        _seconds = 60;
        _timeBtn.enabled = NO;
        [self getSMSNumber];
    }
    if (_seconds > 0) {
        _seconds -- ;
        [_timeBtn setTitle:[NSString stringWithFormat:@"%ld秒后重新获取",(long)_seconds] forState:UIControlStateDisabled];
    }else{
        [_timer invalidate];
        _timeBtn.enabled = YES;
        [_timeBtn setTitle:@"重新获取验证码" forState:UIControlStateNormal];
    }
}
/*
 确认按钮的点击
 */
- (void)confirmClick{
    if (![self.Field.text isEqualToString:_randS]) {
        [self showSVProgressFailed:@"验证码错误"];
        return;
    }
    RegisterThreeViewController *threeVc = [[RegisterThreeViewController alloc] init];
    threeVc.userName = _userName;
    [self.navigationController pushViewController:threeVc animated:YES];
}
/**
 *  验证发送验证码的时间
 */
-(void)getSMSNumber{
    int seconds = (int)[_sendTime timeIntervalSinceNow];
    if (-seconds < 305) {
        //再次发送相同的验证码
        [self sendSms:_randS];
        return;
    }
    _randS = [self randS];
    [self sendSms:_randS];
    //发送验证码
    
}
-(void)sendSms:(NSString *)rand{
    UserVM *userVM = [[UserVM alloc] init];
    [userVM setBlockWithReturnBlock:^(id returnValue) {
       
    } WithErrorBlock:^(id errorCode) {
        _seconds = -1;
        [self showSVProgressFailed:@"获取验证码失败"];
    } WithFailureBlock:^{
        _seconds = -1;
        [self showSVProgressFailed:@"获取验证码失败"];
    }];
    [userVM sendRands:_userName andSMSRand:rand];
}
-(NSString *) randS{
    NSString *strRandom = @"";
    
    for(int i=0; i < 6; i++)
    {
        strRandom = [ strRandom stringByAppendingFormat:@"%i",(arc4random() % 10)];
    }
    
    return strRandom;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
